package com.vilicode.servlet;

import com.vilicode.bean.User;
import com.vilicode.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "HelloServlet", urlPatterns = {"/hello", "/greet"}, loadOnStartup = 1)
@Slf4j
public class HelloServlet extends HttpServlet {
    @Autowired
    private UserService userService;
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        int timeout = session.getMaxInactiveInterval();
        log.info("timeout:{}",timeout);
        User user = (User)session.getAttribute("user");
        log.info("user:{}",user);
        // 设置请求属性
        request.setAttribute("message", "Hello, World!");

        // 转发到 JSP 页面
        request.getRequestDispatcher("/hello.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 处理 POST 请求
        log.info("doPost");
        doGet(request,response);
    }
}